package org.grayrabbit.cms.publish.generate;

import java.io.File;
import java.util.Date;

import org.grayrabbit.cms.entity.Admin;
import org.grayrabbit.cms.entity.PublishLog;
import org.grayrabbit.cms.entity.PublishTask;
import org.grayrabbit.cms.entity.Site;
import org.grayrabbit.cms.publish.api.Generatable;
import org.grayrabbit.cms.util.FilePathSptUtil;

import freemarker.template.Configuration;

/**
 * @author Administrator
 * @version 1.0
 * @created 2011-6-15 10:43:13
 */
public class IndexGenerater extends AbstractGenerater {
	
	private static String htmlFileName = "index.html";
	
	private Site site;
	
	public IndexGenerater(Configuration configuration,Site site,String path,Generatable generatable,Admin admin){
		super(configuration, path,generatable,admin);
		this.site = site;
	}
	
	@Override
	protected File getTargetFile() {
		return new File(FilePathSptUtil.LOCAL_SITE_PUBLISH_PATH+FilePathSptUtil.padRightAndLeftSpt(site.getSiteShortName())+htmlFileName);
	}

	@Override
	protected void initRootMap() {
		rootMap.put("site", site);
	}

	@Override
	protected void logInfo(PublishTask publishTask,boolean success,Throwable t) {
		PublishLog publishLog = new PublishLog();
		publishLog.setHtmlPath(getTargetFile().toString());
		publishLog.setIsError(Boolean.valueOf(!success));
		publishLog.setObjectId(site.getSiteId());
		if(success){
			publishLog.setPublishInfo("生成成功");
		}else{
			if(t!=null){
				publishLog.setPublishInfo("生成失败:"+t.getMessage());
			}else{
				publishLog.setPublishInfo("生成失败");
			}
		}
		publishLog.setPublishStatus(PublishTask.STATUS_FINISHED);
		publishLog.setPublishTime(new Date());
		publishLog.setPublishType("index");
		publishLog.setTemplatePath(this.path);
		publishLog.setPublishTask(publishTask);
		generatable.savePublishLog(publishLog);
		calcTaskInfo(publishTask,success);
	}


}